package cn.springcloud.fix.common.starter.mail;

import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import java.io.File;
import java.util.List;
import java.util.Properties;

/**
 * 邮件发送服务
 */
@Slf4j
public class MailSender {

    private JavaMailSender javaMailSender;

    private FreeMarkerConfigurer freeMarkerConfigurer;

    private String name;

    private String from;

    public MailSender(JavaMailSender javaMailSender, FreeMarkerConfigurer freeMarkerConfigurer, String name, String from) {
        this.name = name;
        this.from = from;
        this.javaMailSender = javaMailSender;
        this.freeMarkerConfigurer = freeMarkerConfigurer;
    }

    /**
     * 发送邮件
     */
    public void sendSimpleMail(MailSenderParams params) {
        try {
            MimeMessage message = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(new InternetAddress(from, MimeUtility.encodeText(this.name, "UTF-8", "B")));
            helper.setTo(params.getMailTo());
            helper.setSubject(params.getTitle());
            helper.setText(params.getContent(), true);
            this.addAttachment(helper, params);

            javaMailSender.send(message);
        } catch (Exception e) {
            throw new RuntimeException("发送邮件异常! from: " + name + "! to: " + params.getMailTo());
        }
    }

    /**
     * 发送html邮件
     */
    public void sendHtmlMail(MailSenderParams params) {
        try {
            MimeMessage message = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(new InternetAddress(from, MimeUtility.encodeText(this.name, "UTF-8", "B")));
            helper.setFrom(from);
            helper.setTo(params.getMailTo());
            helper.setSubject(params.getTitle());
            helper.setText(params.getContent(), true);
            this.addAttachment(helper, params);

            javaMailSender.send(message);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("发送邮件异常! from: " + name + "! to: " + params.getMailTo());
        }
    }

    /**
     * 发送带附件的邮件
     */
    public void sendAttachmentMail(MailSenderParams params) {
        try {
            MimeMessage message = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(new InternetAddress(from, MimeUtility.encodeText(this.name, "UTF-8", "B")));
            helper.setTo(params.getMailTo());
            helper.setSubject(params.getTitle());
            helper.setText(params.getContent(), true);
            this.addAttachment(helper, params);
            javaMailSender.send(message);
        } catch (Exception e) {
            throw new RuntimeException("发送邮件异常! from: " + name + "! to: " + params.getMailTo());
        }
    }

    /**
     * 发送模板邮件
     */
    public void sendTemplateMail(MailSenderParams params) {
        try {
            MimeMessage message = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(new InternetAddress(from, MimeUtility.encodeText(this.name, "UTF-8", "B")));
            helper.setTo(params.getMailTo());
            helper.setSubject(params.getTitle());

            this.addAttachment(helper, params);

            Template template = freeMarkerConfigurer.getConfiguration().getTemplate(params.getTemplateFile());
            String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, params.getTemplateModels());
            helper.setText(html, true);
            javaMailSender.send(message);
        } catch (Exception e) {
            throw new RuntimeException("发送邮件异常! from: " + name + "! to: " + params.getMailTo());
        }
    }

    /**
     * 添加附件
     */
    private void addAttachment(MimeMessageHelper helper, MailSenderParams params) throws MessagingException {
        if (params.getAttachments() != null) {
            List<File> attachments = params.getAttachments();
            for (File file : attachments) {
                FileSystemResource attachment = new FileSystemResource(file);
                helper.addAttachment(file.getName(), file);
            }
        }
    }

   /* public static void main(String[] args) {
        JavaMailSenderImpl js = new JavaMailSenderImpl();
        js.setHost("smtp.qq.com");
        js.setUsername("350006811@qq.com");
        js.setPassword("mygdzwlcjlxpbghc");
        Properties props = new Properties();
        props.put("mail.smtp.auth", true);
        props.put("mail.smtp.ssl.enable", true);
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.timeout", 25000);
        js.setJavaMailProperties(props);
        MimeMessage message = null;
        try {
            message = js.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            // helper.setFrom(new InternetAddress(this.getFrom(), MimeUtility.encodeText(this.name,"UTF-8", "B")));
            helper.setFrom("350006811@qq.com");
            helper.setTo("350006811@qq.com");
            helper.setSubject("测试");
            helper.setText("测试内容");
            //addAttachmentStatic(helper,params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        js.send(message);
    }*/
}
